<template>
  <div class="app-container">
    <el-dialog title="添加员工" :visible="showDialog" width="40%" @close="close">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item prop="username" label="姓名">
          <el-input v-model="form.username" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item prop="mobile" label="手机">
          <el-input v-model="form.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item prop="formOfEmployment" label="聘用形式">
          <!-- <el-input v-model="form.formOfEmployment" placeholder="请输入聘用形式" /> -->
          <el-select v-model="form.formOfEmployment" placeholder="请输入聘用形式">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="workNumber" label="工号">
          <el-input v-model="form.workNumber" placeholder="请输入工号" />
        </el-form-item>
        <el-form-item prop="departmentName" label="组织名称">
          <el-cascader
            v-model="form.departmentName"
            :options="depts"
            placeholder="请输入部门名称"
            :props="defaultProps"
            @focus="getDeptList"
          />
        </el-form-item>
        <el-form-item prop="timeOfEntry" label="入职时间">
          <el-date-picker v-model="form.timeOfEntry" placeholder="请选择入职时间" />
        </el-form-item>
        <el-form-item prop="correctionTime" label="转正时间	">
          <el-date-picker v-model="form.correctionTime" placeholder="请选择转正时间" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="add">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import EmployeeEnum from '@/api/constant/employees'
import { getDeptList } from '@/api/dept'
import { listTotree } from '@/utils/index'
import { addEmployee } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      defaultProps: {
        // 拿到的值是部门名称 默认是label:label value :value 所以要修改
        label: 'name',
        value: 'name',
        expandTrigger: 'hover'
      },
      depts: [],
      value: '',
      options: EmployeeEnum.hireType,
      form: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: ['blur', 'change'] },
          { pattern: /^[\u4e00-\u9fa5]{2,4}$/, message: '用户姓名为2-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    // 1.获取部门
    async getDeptList() {
      const res = await getDeptList()
      console.log(res)
      this.depts = listTotree(res.depts)
    },
    // 2.关闭弹窗
    close() {
      this.$emit('update:showDialog', false)
    },
    // 3.点击添加
    async add() {
      try {
        // 校验表单
        await this.$refs.form.validate()
      } catch {
        return
      }
      // 发送请求 因为部门获取到的是数组
      await addEmployee({ ...this.form, departmentName: this.form.departmentName.slice(-1)[0] })
      this.$message.success('添加成功')
      this.$emit('addEmp')
    }
  }
}
</script>

<style>

</style>
